package com.sunny.ynhz.ui.patrol.problems;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ChooseRiverAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.ChosseRiverBean;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.utils.SPUtil;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by luao on 2017/8/4.
 */

public class ChooseRiverActivity extends AppBaseTranslationTitleBarActivity {


    @BindView(R.id.recycler)
    RecyclerView recycler;
    private ChooseRiverAdapter chooseRiverAdapter;
    private LinkedList<ChosseRiverBean.DataBean> riverChooseList;


    public static void ToChooseRiverActivity(Activity context) {
        Intent intent = new Intent(context, ChooseRiverActivity.class);
        context.startActivityForResult(intent, 400);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_river);
        ButterKnife.bind(this);
        setTitle("选择河道");

        riverChooseList = new LinkedList<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(linearLayoutManager);
        chooseRiverAdapter = new ChooseRiverAdapter(riverChooseList);
        recycler.setAdapter(chooseRiverAdapter);
        getData(SPUtil.get("userId","null").toString());
    }

    private void getData(String userId) {
        HttpManager.getInstance()
                .getRiverList(userId)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiSubscriber(this, new ApiSubscriber.HttpOnResultListener<ChosseRiverBean>() {
                    @Override
                    public void onNext(ChosseRiverBean jsonObject) {
                        if (jsonObject.getData() != null) {
                            riverChooseList.clear();
                            riverChooseList.addAll(jsonObject.getData());
                            chooseRiverAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onstart() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }
}
